package com.example.light.web.system.service;


import com.example.light.web.system.dao.DepartmentMapper;
import com.example.light.web.system.entity.Department;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;


import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class DeptService {

    @Resource
    private DepartmentMapper deptMapper;

    //查询部门列表
    public String  Queryall(Model model){
        List<Department> departments=deptMapper.Querall();
        model.addAttribute("depts",departments);
        return "system/department";
    }

    //添加部门界面
    private int TempId;//界面ID
    public String dpetAddView(Model model, Integer deptId){
        if(null != deptId){
            TempId=deptId+1;
        }
        model.addAttribute("deptId",TempId);
        return "system/departmentadd";
    }

    //添加部门
    public String deptAdd(Model model,Department department){
        Date date=new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        System.out.println(TempId);
        System.out.println(timestamp);
        //department.setDeptId(TempId);
        department.setCreateTime(timestamp);
        if(department.getDeptName()==""){
            return "f";
        }
        int flag=deptMapper.add(department);
        if(flag==1){
            return "t";
        }else{
            return "f";
        }
    }


    //删除部门（多个）
    public String deptDeletM(Model model,Integer [] ids){
        for(Integer id:ids){
           deptMapper.deleteByPrimaryKey(id);
        }
        //deptMapper.deletM(ids);
        List<Department> departments=deptMapper.Querall();
        model.addAttribute("depts",departments);
        return "system/department";
    }



    //部门修改界面
    private Department TempDepartment; //界面数据
    public String dpetUpdateView(Model model,Department dept){
        if(null != dept.getDeptId()){
            TempDepartment=dept;
        }
        model.addAttribute("depts",TempDepartment);
        return "system/departmentupdate";
    }


    //部门修改
    public String deptUpdate(Model model,Department department){
        Date date=new Date(System.currentTimeMillis());
        department.setCreateTime(date);
        if(department.getDeptName()==""){
            return "f";
        }
        int flag=deptMapper.update(department);
        if(flag==1){
            return "t";
        }else{
            return "f";
        }
    }
}
